<?php

if (!defined("BASEPATH"))
    exit("No direct script access allowed");
require_once APPPATH . "controllers/dienhoa-administrator/base_controller" . EXT;

class Hoa_don extends Base_controller {

    var $limit = 20;

    public function __construct() {
        parent::__construct();
        $this->check_login();
    }

    public function hoa_don_all() {

        $this->permissions_view();
        $data = array();
        //set menu
        $data = $this->SetMenu("HoaDon");
        $total_seg = $this->uri->total_segments();
//config pagination

        if (isset($_POST["btnSend"]) || $this->input->get("pg") === "search") {
            if (isset($_POST["btnSend"])) {
                $query_array = array(
                    "MHD" => $this->input->post("txtMHD", TRUE),
                    "KH" => $this->input->post("txtKH", TRUE),
                    "ODate" => $this->input->post("txtODate"),
                    "OSDate" => $this->input->post("txtOSDate")
                );
            }
            if ($this->input->get("pg") === "search") {
                $query_array = array(
                    "MHD" => ($this->input->get("MHD", TRUE)) ? $this->input->get("MHD", TRUE) : null,
                    "KH" => ($this->input->get("KH", TRUE)) ? $this->input->get("KH", TRUE) : null,
                    "ODate" => ($this->input->get("ODate", TRUE)) ? $this->input->get("ODate", TRUE) : null,
                    "OSDate" => ($this->input->get("OSDate", TRUE)) ? $this->input->get("OSDate", TRUE) : null
                );
            }
            $base_url = base_url() . "dienhoa-administrator/hoa_don/hoa_don_all/?pg=search&MHD=" . $query_array["MHD"] . "&KH=" . $query_array["KH"] . "&ODate=" . $query_array["ODate"] . "&OSDate=" . $query_array["OSDate"];

            $result = $this->order_model->GetOrder($query_array, $this->limit, $this->input->get("page"));
        } else if (!isset($_POST["btnSend"]) || $this->input->get("pg") === "hd") {

            $base_url = base_url() . "dienhoa-administrator/hoa_don/hoa_don_all/?pg=hd";
            $result = $this->order_model->GetOrder(null, $this->limit, $this->input->get("page"));
        }

        $data["hoa_don"] = $result["rows"];
        $data['num_results'] = $result['num_rows'];
//cofig
        $config = array();
        $config['base_url'] = $base_url;
        $config['total_rows'] = $data['num_results'];
        $config['per_page'] = $this->limit;
        $config["page_query_string"] = TRUE;
        $config["query_string_segment"] = "page";
        $this->cfPag();

        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $this->render($this->load->view("admin/order/hoa_don_view", $data, TRUE));
    }

    public function hoa_don_trong_ngay() {

        $this->permissions_view();
        $data = array();
        //set menu
        $data = $this->SetMenu("HoaDon");

        $total_seg = $this->uri->total_segments();
//config pagination
        $ngay = date("d");
        $thang = date("m");
        $nam = date("Y");
        $curdate = $nam . "-" . $thang . "-" . $ngay;
        if (isset($_POST["btnSend"]) || $this->input->get("pg") === "search") {
            if (isset($_POST["btnSend"])) {
                $query_array = array(
                    "MHD" => $this->input->post("txtMHD", TRUE),
                    "KH" => $this->input->post("txtKH", TRUE),
                    "ODate" => $this->input->post("txtODate"),
                    "OSDate" => $this->input->post("txtOSDate")
                );
            }
            if ($this->input->get("pg") === "search") {
                $query_array = array(
                    "MHD" => ($this->input->get("MHD", TRUE)) ? $this->input->get("MHD", TRUE) : null,
                    "KH" => ($this->input->get("KH", TRUE)) ? $this->input->get("KH", TRUE) : null,
                    "ODate" => ($this->input->get("ODate", TRUE)) ? $this->input->get("ODate", TRUE) : null,
                    "OSDate" => ($this->input->get("OSDate", TRUE)) ? $this->input->get("OSDate", TRUE) : null
                );
            }
            $base_url = base_url() . "dienhoa-administrator/hoa_don/hoa_don_all/?pg=search&MHD=" . $query_array["MHD"] . "&KH=" . $query_array["KH"] . "&ODate=" . $query_array["ODate"] . "&OSDate=" . $query_array["OSDate"];

            $result = $this->order_model->HdTrongNgay($query_array, $this->limit, $this->input->get("page"), $curdate);
        } else if (!isset($_POST["btnSend"]) || $this->input->get("pg") === "hd") {

            $base_url = base_url() . "dienhoa-administrator/hoa_don/hoa_don_all/?pg=hd";
            $result = $this->order_model->HdTrongNgay(null, $this->limit, $this->input->get("page"), $curdate);
        }

        $data["hoa_don"] = $result["rows"];
        $data['num_results'] = $result['num_rows'];
//cofig
        $config = array();
        $config['base_url'] = $base_url;
        $config['total_rows'] = $data['num_results'];
        $config['per_page'] = $this->limit;
        $config["page_query_string"] = TRUE;
        $config["query_string_segment"] = "page";
        $this->cfPag();

        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $this->render($this->load->view("admin/order/hoa_don_trong_ngay_view", $data, TRUE));
    }

    public function hd_chua_xu_ly() {
        $this->permissions_view();
        $data = array();
        //set menu
        $data = $this->SetMenu("HoaDon");

        $total_seg = $this->uri->total_segments();
//config pagination
        if (isset($_POST["btnSend"]) || $this->input->get("pg") === "search") {
            if (isset($_POST["btnSend"])) {
                $query_array = array(
                    "MHD" => $this->input->post("txtMHD", TRUE),
                    "KH" => $this->input->post("txtKH", TRUE),
                    "ODate" => $this->input->post("txtODate"),
                    "OSDate" => $this->input->post("txtOSDate")
                );
            }
            if ($this->input->get("pg") === "search") {
                $query_array = array(
                    "MHD" => ($this->input->get("MHD", TRUE)) ? $this->input->get("MHD", TRUE) : null,
                    "KH" => ($this->input->get("KH", TRUE)) ? $this->input->get("KH", TRUE) : null,
                    "ODate" => ($this->input->get("ODate", TRUE)) ? $this->input->get("ODate", TRUE) : null,
                    "OSDate" => ($this->input->get("OSDate", TRUE)) ? $this->input->get("OSDate", TRUE) : null
                );
            }
            $base_url = base_url() . "dienhoa-administrator/hoa_don/hoa_don_all/?pg=search&MHD=" . $query_array["MHD"] . "&KH=" . $query_array["KH"] . "&ODate=" . $query_array["ODate"] . "&OSDate=" . $query_array["OSDate"];

            $result = $this->order_model->HdChuaXuLy($query_array, $this->limit, $this->input->get("page"));
        } else if (!isset($_POST["btnSend"]) || $this->input->get("pg") === "hd") {

            $base_url = base_url() . "dienhoa-administrator/hoa_don/hoa_don_all/?pg=hd";
            $result = $this->order_model->HdChuaXuLy(null, $this->limit, $this->input->get("page"));
        }

        $data["hoa_don"] = $result["rows"];
        $data['num_results'] = $result['num_rows'];
//cofig
        $config = array();
        $config['base_url'] = $base_url;
        $config['total_rows'] = $data['num_results'];
        $config['per_page'] = $this->limit;
        $config["page_query_string"] = TRUE;
        $config["query_string_segment"] = "page";
        $this->cfPag();

        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $this->render($this->load->view("admin/order/hd_chua_xl_view", $data, TRUE));
    }

    public function hd_da_xl() {
        $this->permissions_view();
        $data = array();
        //set menu
        $data = $this->SetMenu("HoaDon");

        $total_seg = $this->uri->total_segments();
//config pagination
        if (isset($_POST["btnSend"]) || $this->input->get("pg") === "search") {
            if (isset($_POST["btnSend"])) {
                $query_array = array(
                    "MHD" => $this->input->post("txtMHD", TRUE),
                    "KH" => $this->input->post("txtKH", TRUE),
                    "ODate" => $this->input->post("txtODate"),
                    "OSDate" => $this->input->post("txtOSDate")
                );
            }
            if ($this->input->get("pg") === "search") {
                $query_array = array(
                    "MHD" => ($this->input->get("MHD", TRUE)) ? $this->input->get("MHD", TRUE) : null,
                    "KH" => ($this->input->get("KH", TRUE)) ? $this->input->get("KH", TRUE) : null,
                    "ODate" => ($this->input->get("ODate", TRUE)) ? $this->input->get("ODate", TRUE) : null,
                    "OSDate" => ($this->input->get("OSDate", TRUE)) ? $this->input->get("OSDate", TRUE) : null
                );
            }
            $base_url = base_url() . "dienhoa-administrator/hoa_don/hoa_don_all/?pg=search&MHD=" . $query_array["MHD"] . "&KH=" . $query_array["KH"] . "&ODate=" . $query_array["ODate"] . "&OSDate=" . $query_array["OSDate"];

            $result = $this->order_model->HdDaXuLy($query_array, $this->limit, $this->input->get("page"));
        } else if (!isset($_POST["btnSend"]) || $this->input->get("pg") === "hd") {

            $base_url = base_url() . "dienhoa-administrator/hoa_don/hoa_don_all/?pg=hd";
            $result = $this->order_model->HdDaXuLy(null, $this->limit, $this->input->get("page"));
        }

        $data["hoa_don"] = $result["rows"];
        $data['num_results'] = $result['num_rows'];
//cofig
        $config = array();
        $config['base_url'] = $base_url;
        $config['total_rows'] = $data['num_results'];
        $config['per_page'] = $this->limit;
        $config["page_query_string"] = TRUE;
        $config["query_string_segment"] = "page";
        $this->cfPag();

        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $this->render($this->load->view("admin/order/hd_da_xl_view", $data, TRUE));
    }

    public function hddxl() {
        $this->permissions_view();
        $data = array();
        //set menu
        $data = $this->SetMenu("HoaDon");

        $total_seg = $this->uri->total_segments();
//config pagination
        if (isset($_POST["btnSend"]) || $this->input->get("pg") === "search") {
            if (isset($_POST["btnSend"])) {
                $query_array = array(
                    "MHD" => $this->input->post("txtMHD", TRUE),
                    "KH" => $this->input->post("txtKH", TRUE),
                    "ODate" => $this->input->post("txtODate"),
                    "OSDate" => $this->input->post("txtOSDate")
                );
            }
            if ($this->input->get("pg") === "search") {
                $query_array = array(
                    "MHD" => ($this->input->get("MHD", TRUE)) ? $this->input->get("MHD", TRUE) : null,
                    "KH" => ($this->input->get("KH", TRUE)) ? $this->input->get("KH", TRUE) : null,
                    "ODate" => ($this->input->get("ODate", TRUE)) ? $this->input->get("ODate", TRUE) : null,
                    "OSDate" => ($this->input->get("OSDate", TRUE)) ? $this->input->get("OSDate", TRUE) : null
                );
            }
            $base_url = base_url() . "dienhoa-administrator/hoa_don/hoa_don_all/?pg=search&MHD=" . $query_array["MHD"] . "&KH=" . $query_array["KH"] . "&ODate=" . $query_array["ODate"] . "&OSDate=" . $query_array["OSDate"];

            $result = $this->order_model->HdDangXuLy($query_array, $this->limit, $this->input->get("page"));
        } else if (!isset($_POST["btnSend"]) || $this->input->get("pg") === "hd") {

            $base_url = base_url() . "dienhoa-administrator/hoa_don/hoa_don_all/?pg=hd";
            $result = $this->order_model->HdDangXuLy(null, $this->limit, $this->input->get("page"));
        }

        $data["hoa_don"] = $result["rows"];
        $data['num_results'] = $result['num_rows'];
//cofig
        $config = array();
        $config['base_url'] = $base_url;
        $config['total_rows'] = $data['num_results'];
        $config['per_page'] = $this->limit;
        $config["page_query_string"] = TRUE;
        $config["query_string_segment"] = "page";
        $this->cfPag();

        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $this->render($this->load->view("admin/order/hd_dang_xuly_view", $data, TRUE));
    }

    public function xoa() {
        $this->permission_del_edit();
        $dat = $this->input->post("item");
        for ($i = 0; $i < sizeof($dat); $i++) {
            $delete = $this->order_model->DeleteOrder($dat[$i]);
            if ($delete != 0) {
                echo "<script>alert('Hóa đơn chưa xử lý xong không thể xóa!'); window.history.back();</script>";
            }
        }
        redirect("dienhoa-administrator/hoa_don/hoa_don_all", "refresh");
    }

    public function chi_tiet_hoa_don() {
        $this->permissions_view();
        $data = array();
        $data = $this->SetMenu("HoaDon");
        $oid = $this->uri->segment(4);
        $data["hd_view"] = $this->order_model->GetOrderToID($oid);
        $data["pay"] = $this->pay_model->GetPay2($data["hd_view"]->PayID);
        $data["kh"] = $this->user_model->getUserToID($data["hd_view"]->CID)->row();
        $data["hd_ct"] = $this->order_model->GetOrderDetail($oid)->result();
        $this->render($this->load->view("admin/order/order_detail_view", $data, TRUE));
    }

    public function sua_TtHD() {
        $this->permission_del_edit();
        $oid = $this->uri->segment(4);
        $stt = $this->uri->segment(5);

        $updateSTT = $this->order_model->UpdateStt($oid, $stt);
        if ($updateSTT)
            echo "<script>window.history.back();</script>";
        else
            echo "<script>alert('khong cap nhat duoc'); window.history.back();</script>";
    }

}

?>
